package com.xuebang.admin.util;

import java.io.StringWriter;

import org.codehaus.jackson.map.ObjectMapper;

public final class JsonUtil {
	@SuppressWarnings("serial")
	public static class CodecException extends RuntimeException {
		public CodecException(Throwable cause) {
			super(cause);
		}
	}

	private JsonUtil() {
	}

	public static String dump(Object obj) throws CodecException {
		ObjectMapper mapper = new ObjectMapper();
		StringWriter writer = new StringWriter();
		try {
			mapper.writeValue(writer, obj);
		} catch (Exception e) {
			throw new CodecException(e);
		}
		return writer.toString();
	}

	public static <T> T load(String json, Class<T> type) throws CodecException {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.readValue(json, type);
		} catch (Exception e) {
			throw new CodecException(e);
		}
	}

}
